define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable'], function ($, undefined, Backend, Table, Form, undefined) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'yexam/combo_category/index' + location.search,
                    add_url: 'yexam/combo_category/add',
                    edit_url: 'yexam/combo_category/edit',
                    del_url: 'yexam/combo_category/del',
                    //table: 'question_type',
                },
                pageList : [20,40,60,80]
            });

            var table = $("#question_type");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                escape: false,
                pk: 'category_id',
                sortName: 'sort',
                sortOrder:'asc',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'sort', title: "排序",operate: false, },
                        {field: 'category_id', title: __('Id')},
                        {field: 'sort', title: __('权重'),align: 'left',width:'60px'},
                        {field: 'category_name', title: __('分类名称'),align: 'left', editable: true},
                        {field: 'exam_time', title: __('考试时间'),align: 'left', editable: true},
                        {field: 'create_time', title: __('创建时间'),operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                queryParams:function (params) {
                    /*let filter = JSON.parse(params.filter);
                    let op = JSON.parse(params.op);
                    filter.identify = 0;
                    op.identify = '=';
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);*/
                    return params;
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#c-exam_time").on("dp.update", function(){
                console.log('update');
            });

            Controller.api.bindevent([]);
        },
        edit: function () {
            //================ 获取id数组
            let str = $("#question_type_id").val();
            Controller.api.bindevent(str.split(','));
        },
        api: {
            formatter:{

            },
            bindevent: function (arrs) {

                $("#c-package_ids").data("params", function (obj) {
                    return {custom: {category_id: $("#c-category_id").val()}};
                });

                let questionType = arrs;
                //=====================  选择题型 ===================
                $("#question-type input").change(function () {
                    let id = $(this).val();
                    let code = $(this).attr('data-code');
                    if ($(this).prop('checked')) {
                        //============== 添加题型
                        questionType.push(id);
                        let name = $(this).siblings().html();
                        let tpl = `<div class="form-group" id="iselectallnumber_${code}">
        <label for="iselectallnumber_${code}" class="control-label col-xs-12 col-sm-2">${name}</label>
        <div class="col-xs-12 col-sm-8 form-inline">
            <input type="hidden" name="row[extend][${code}][name]" value="${name}">
            <span class="info">共</span>
            <input class="form-control" type="text"  name="row[extend][${code}][number]" value="0"/>
            <span class="info">题，每题</span><input class="form-control"  type="text" name="row[extend][${code}][score]" value="1" >
            <span class="info">分，</span>
            <span class="info">易</span><input class="form-control" type="text" name="row[extend][${code}][easynumber]" value="0" >
            <span class="info">中</span><input class="form-control" type="text"  name="row[extend][${code}][middlenumber]" value="0" >
            <span class="info">难</span><input class="form-control" type="text"  name="row[extend][${code}][hardnumber]" value="0" >
        </div>
    </div>`;
                        $(".question-type-list-dynamic").append(tpl);
                    }else
                    {
                        //=================== 移除题型
                        questionType.splice(questionType.indexOf(id),1);
                        $("#iselectallnumber_"+code).remove();
                    }
                    $("#question_type_id").val(questionType.toString());
                });

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
